﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Faz.SideWinderSC.Logic;

namespace Faz.SideWinderSC.Client.ViewModel
{
    /// <summary>
    /// Defines the view-model of the application.
    /// </summary>
    public class ConfigurationViewModel : AbstractViewModel
    {
        /// <summary>
        /// The available controller models.
        /// </summary>
        private readonly ICollection<ModelViewModel> models;

        /// <summary>
        /// The active controllers.
        /// </summary>
        private readonly ICollection<SwscViewModel> activeControllers;

        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationViewModel"/> class.
        /// </summary>
        public ConfigurationViewModel()
        {
            this.models = new ObservableCollection<ModelViewModel>();
            this.activeControllers = new ObservableCollection<SwscViewModel>();
            
            this.Initialize();
        }

        /// <summary>
        /// Gets the available controller models.
        /// </summary>
        public ICollection<ModelViewModel> Models
        {
            get { return this.models; }
        }

        /// <summary>
        /// Gets the active controllers.
        /// </summary>
        public ICollection<SwscViewModel> ActiveControllers
        {
            get { return this.activeControllers; }
        }

        /// <summary>
        /// Gets or sets the current controller.
        /// </summary>
        public SwscViewModel CurrentController
        {
            get { return this.GetValue<SwscViewModel>("CurrentController"); }
            set { this.SetValue("CurrentController", value); }
        }

        /// <summary>
        /// Refreshes the profiles and controllers lists.
        /// </summary>
        public void Initialize()
        {
            // Reloal all the model
            this.Models.Clear();
            foreach (var model in ServiceFactory.Instance.ModelService.RetrieveModels())
            {
                this.Models.Add(new ModelViewModel(model));
            }

            // Reload all the active controllers
            this.ActiveControllers.Clear();
            foreach (var controller in SwscController.RetrieveAll())
            {
                this.ActiveControllers.Add(new SwscViewModel(controller));
            }

            // Set the current controller
            this.CurrentController = this.ActiveControllers.FirstOrDefault();
        }
    }
}
